﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tools : MonoBehaviour
{
    public static Tools Instance { get; private set; }
    private Dictionary<Monster, float> horizontalMoveMonsters = new Dictionary<Monster, float>();
    private Dictionary<Monster, KeyValuePair<Vector3, float>> verticalMoveMonsters = new Dictionary<Monster, KeyValuePair<Vector3, float>>();



    private void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
    }


    private void Update()
    {
        List<Monster> horizontalMonsters = new List<Monster>(horizontalMoveMonsters.Keys);
        for(int i = 0;i != horizontalMonsters.Count; ++i)
        {
            Monster monster = horizontalMonsters[i];
            float horizontal;
            horizontalMoveMonsters.TryGetValue(monster, out horizontal);
        
            float prevPosX = monster.transform.position.x;
            if (horizontal < 0)
            {
                monster.HorizontalMoveRun(Vector3.left);
                horizontalMoveMonsters[monster] += prevPosX - monster.transform.position.x;
                if(horizontalMoveMonsters[monster] > 0)
                {
                    horizontalMoveMonsters[monster] = 0;
                }
            }
            else if (horizontal > 0)
            {
                monster.HorizontalMoveRun(Vector3.right);
                horizontalMoveMonsters[monster] -= monster.transform.position.x - prevPosX;
                if (horizontalMoveMonsters[monster] < 0)
                {
                    horizontalMoveMonsters[monster] = 0;
                }
            }

            if (horizontal == 0)
            {
                horizontalMoveMonsters.Remove(monster);
            }
        }


        float verticalMoveSpeed = 16.0f;
        List<Monster> verticalMonsters = new List<Monster>(verticalMoveMonsters.Keys);
        for (int i = 0; i != verticalMonsters.Count; ++i)
        {
            Monster monster = verticalMonsters[i];
            KeyValuePair<Vector3, float> pair;
            verticalMoveMonsters.TryGetValue(monster, out pair);

            Vector3 vertical = pair.Key;
            float high = pair.Value;
         
            float prevPosY = monster.transform.position.y;
            if (high < 0)
            {
                monster.transform.Translate(vertical * verticalMoveSpeed * Time.deltaTime, Space.World);
                float currHigh = high + (prevPosY - monster.transform.position.y);
                if (currHigh > 0)
                {
                    currHigh = 0;
                }

                verticalMoveMonsters[monster] = new KeyValuePair<Vector3, float>(vertical, currHigh);
            }
            else if(high > 0)
            {
                monster.transform.Translate(vertical * verticalMoveSpeed * Time.deltaTime, Space.World);
                float currHigh = high - (monster.transform.position.y - prevPosY);
                if (currHigh < 0)
                {
                    currHigh = 0;
                }

                verticalMoveMonsters[monster] = new KeyValuePair<Vector3, float>(vertical, currHigh);
            }

            if (high == 0)
            {
                verticalMoveMonsters.Remove(monster);
            }
        }
    }


    //水平移动horizontal 个单位
    public void HorizontalMove(Monster monster, float horizontal)
    {
        if (horizontalMoveMonsters.ContainsKey(monster))
        {
            horizontalMoveMonsters[monster] = horizontal;
        }
        else
        {
            horizontalMoveMonsters.Add(monster, horizontal);
        }
    }


    //垂直移动, 该版本默认以Vector3.up 的方向跳跃
    public void VerticalMove(Monster monster)
    {
        VerticalMove(monster, Vector3.up, monster.jumpHigh);
    }


    //vertical 方向移动high 个单位
    public void VerticalMove(Monster monster, Vector3 vertical, float high)
    {
        if (verticalMoveMonsters.ContainsKey(monster))
        {
            verticalMoveMonsters[monster] = new KeyValuePair<Vector3, float>(vertical, high);
        }
        else
        {
            verticalMoveMonsters.Add(monster, new KeyValuePair<Vector3, float>(vertical, high));
        }
    }
}
